package io.lkt.modules.sys.controller;

import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.BUserOpenidsLog;
import io.lkt.modules.sys.service.BUserOpenidsService;
import io.lkt.modules.sys.service.SysGzhService2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/sys/buseropenidslog"})
public class BUserOpenidsController {
    @Autowired
    private BUserOpenidsService bUserOpenidsService;

    @Autowired
    private SysGzhService2 sysGzhService2;

    @RequestMapping({"/queryList"})
    //@RequiresPermissions({"sys:buseropenidslog:list"})
    public R queryList(@RequestParam Map<String, Object> params) {
        if(params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }
        //System.out.println("输出：" + params);
        PageUtils page = bUserOpenidsService.getUserOpenidsLogs(params);
        return R.ok().put("page", page);
    }

    /**
     * 公众号小时关注排行榜
     */
    @RequestMapping("/rankFollow")
    public R rankFollow(@RequestBody Map<String, Object> params) {

        if (params.get("pDate") != null && !params.get("pDate").toString().trim().equals("")) {
            params.put("pDate", params.get("pDate"));
        } else {
            params.put("pDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()));
        }

        List<BUserOpenidsLog> list = bUserOpenidsService.rankFollow(params);

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            category.add(list.get(i).getHour());
            bar0.add(list.get(i).getComparisonFollowCount()); // 关注数
            bar1.add(list.get(i).getComparisonUnfollowCount()); // 取关数
            bar2.add(list.get(i).getComparison()); // 净增数
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }

    @RequestMapping("/getCurrentMonthFansCount")
    public R getCurrentMonthFansCount(@RequestBody Map<String, Object> params) {
        if (params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.error("访问失败！");
        }
        if (params.get("currentDate") != null && !params.get("currentDate").toString().trim().equals("")) {
            params.put("currentDate", params.get("currentDate"));
        } else {
            params.put("currentDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()));
        }
        int current = bUserOpenidsService.getCurrentMonthFollow(params);// 公众号本月粉丝数量
        int target = sysGzhService2.getCurrentMonthTarget(params.get("gzhCode").toString()); // 公众号本月设置目标粉丝数
        // 完成百分比
        double result = (current * 10000) / target;
        BigDecimal bd = new BigDecimal(result / 100);
        bd = bd.setScale(2, RoundingMode.HALF_UP);

        return R.ok().put("current", current)
                .put("target", target)
                .put("percent", bd.toString());
    }
}
